package com.mairuide.openaccountinfo.service;

import com.mairuide.openaccountinfo.dao.OpenaccountInfoDao;
import com.mairuide.openaccountinfo.pojo.OpenaccountInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by 敲代码的卡卡罗特
 * on 2019/1/10 10:52.
 */
@Service
public class OpenaccountInfoService {
    @Autowired
    private OpenaccountInfoDao openaccountInfoDao;

    @Resource
    JdbcTemplate jdbcTemplate;

    public void batchSave(List<OpenaccountInfo> list) {
        if(list!=null && list.size()>0){
            final List<OpenaccountInfo> tempList = list;
            String sql="insert into TB_OPENACCOUNT_INFO(ID, ACCOUNT_OWNER, ID_NUMBER, TRADE_CARD, TRADE_ACCOUNT, OPEN_ACCOUNT_TIME," +
                    "CASEMANAGE_ID,CURRENCY,ACCOUNT_OPEN,ACCOUNT_STATUS,ACCOUNT_TYPE," +
                    "TELEPHONE,OUTLETS,OUTLETS_TIME) ";
            sql+=" values(SEQ_OPENACCOUNT_INFO.nextval,?,?,?,?,?," +
                    "?,?,?,?,?," +
                    "?,?,?) ";
            jdbcTemplate.batchUpdate(sql,
                    new BatchPreparedStatementSetter() {
                        public void setValues(PreparedStatement ps, int i)
                                throws SQLException {
                            OpenaccountInfo info = tempList.get(i);
                            ps.setString(1, info.getAccountOwner()==null?"":info.getAccountOwner());
                            ps.setString(2, info.getIdNumber()==null?"":info.getIdNumber());
                            ps.setString(3, info.getTradeCard()==null?"":info.getTradeCard());
                            ps.setString(4, info.getTradeAccount()==null?"":info.getTradeAccount());
                            ps.setTimestamp(5, info.getOpenAccountTime()==null?null:info.getOpenAccountTime());

                            ps.setString(6, info.getCasemanageId()==null?null:info.getCasemanageId());
                            ps.setString(7, info.getCurrency()==null?null:info.getCurrency());
                            ps.setString(8, info.getAccountOpen()==null?null:info.getAccountOpen());
                            ps.setString(9, info.getAccountStatus()==null?null:info.getAccountStatus());
                            ps.setString(10, info.getAccountType()==null?null:info.getAccountType());

                            ps.setString(11, info.getTelephone()==null?null:info.getTelephone());
                            ps.setString(12, info.getOutlets()==null?null:info.getOutlets());
                            ps.setTimestamp(13, info.getOutletsTime()==null?null:info.getOutletsTime());
                        }
                        public int getBatchSize() {
                            return tempList.size();
                        }
                    });
        }
    }

    public void deleteByCasemanageId(String casemanageId) {
        String sql= "delete from TB_OPENACCOUNT_INFO where CASEMANAGE_ID = "+ casemanageId;
        jdbcTemplate.execute(sql);
    }
}
